package com.leo.erp.web.person;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leo.erp.dto.PersonExecution;
import com.leo.erp.entity.Person;
import com.leo.erp.enums.PersonStateEnum;
import com.leo.erp.service.PersonService;
import com.leo.erp.util.HttpServletRequestUtil;

@RequestMapping(value = "/person")
@Controller
public class PersonManagementController {
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		String name = HttpServletRequestUtil.getString(request, "name");
		String type = HttpServletRequestUtil.getString(request, "type");
		
		PersonExecution pe = personService.getList(name, type, currentPage, pageSize);
		if(pe.getState() == PersonStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
			modelMap.put("pages",pe.getPages());//总页数
			modelMap.put("total", pe.getTotal());//总数
			
			Map<String, Object> map = new HashMap<>();
			map.put("rows", pe.getPersonPageList());
			map.put("total", pe.getTotal());
			//modelMap.put("userList", ue.getUserPageList());
			modelMap.put("personList", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询列表失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		int personId = HttpServletRequestUtil.getInt(request, "id");
		
		PersonExecution pe = personService.updatePerson(beanJson, personId, request);
		if(pe.getState() == PersonStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	/**
	 * 插入经手人信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		PersonExecution pe = personService.insertPerson(beanJson, request);
		if(pe.getState() == PersonStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
		}
		
		return modelMap;
	}
	
	/**
	 * 批量删除经手人信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/batchDeletePersonByIds")
	@ResponseBody
	public Map<String, Object> batchDeletePersonByIds(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String ids = HttpServletRequestUtil.getString(request, "ids");
		String deleteType = HttpServletRequestUtil.getString(request, "deleteType");//1:正常删除,2:强制删除
		PersonExecution pe = new PersonExecution();
		if("1".equals(deleteType)) {
			//正常删除
			pe = personService.batchDeletePersonByIdsNormal(ids);
			if(pe.getState() == PersonStateEnum.DELETE.getState()) {
				modelMap.put("success", false);
				modelMap.put("code", 2);
				modelMap.put("msg", "希望强制删除吗?");
			}else if(pe.getState() == PersonStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除经手人信息失败");
			}
		}else if("2".equals(deleteType)) {
			//强制删除
			int effectedNum = personService.batchDeleteSupplierByIds(ids);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除经手人信息失败");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除操作被拒绝，请联系管理员");
		}
		
		return modelMap;
	}
	
	/**
     * 根据类型获取经手人信息 1-业务员，2-仓管员，3-财务员
     * @param typeNum
     * @param request
     * @return
     */
	@RequestMapping(value = "/getPersonByNumType")
	@ResponseBody
	public JSONArray getPersonByNumType(HttpServletRequest request){
		JSONArray dataArray = new JSONArray();
		//获取前台传过来的值
		String typeNum = HttpServletRequestUtil.getString(request, "type");
		String type = "";
        if (typeNum.equals("1")) {
            type = "业务员";
        } else if (typeNum.equals("2")) {
            type = "仓管员";
        } else if (typeNum.equals("3")) {
            type = "财务员";
        }
        PersonExecution pe = personService.getPersonByType(type);
        if(pe.getState() == PersonStateEnum.SUCCESS.getState()) {
        	for(Person person : pe.getPersonList()) {
        		JSONObject item = new JSONObject();
                item.put("id", person.getPersonId());
                item.put("name", person.getName());
                dataArray.add(item);
        	}
        }
		
		return dataArray;
	}
}
